/**
 * JMS规范中的消息类型：TextMessage、MapMessage、ObjectMessage、BytesMessage和StreamMessage等五种
 * 1.建立ConnectionFactory工厂对象，需要填入用户名、密码、连接地址（一般使用默认，如果没有修改的话）
 * 2.创建一个Connection连接，并且调用Connection的start方法开启连接，Connection方法默认是关闭的
 * 3.创建Session会话(connection.createSession)，用于接收消息，参数1是是否启用事务，参数2是签收模式，一般设置为自动签收
 * 4.创建Destination队列(session.createQueue)，指的是一个客户端用来制定生产消息目标和消费消息来源的对象。在PTP的模式中，Destination被称作队列，在Pub/Sub模式中，Destination被称作主题（Topic）
 * 5.创建消息消费者MessageConsumer (session.createConsumer)
 * 6.接收消息(consumer.receive或者consumer.setMessageListener)
 * 7.释放资源session.close、connection.close
 *
 * 同步接收消息（MessageConsumer）：
 * 1）Message receive()
 * 2）Message receive(long timeout)
 * 3）Message receiveNoWait()
 * 异步接收消息（MessageConsumer）：
 * setMessageListener(MessageListener listener)
 */
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class QueueConsumer {
    public static void main(String[] args) throws JMSException {
        Connection connection = null;
        Session session = null;
        /*等效替换
        QueueConnection connection = null;
        QueueSession session = null;*/
        try {
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                    ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("myqueue");
            MessageConsumer consumer = session.createConsumer(destination);
             /*等效替换
            QueueConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                    ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");
            connection = connectionFactory.createQueueConnection();
            connection.start();
            session = connection.createQueueSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue("myqueue");
            QueueReceiver consumer = session.createReceiver(queue);
             */
            //同步接收消息
            while (true) {
                // 接收数据的时间（等待） 100 ms
                TextMessage message = (TextMessage) consumer.receive(1000 * 100);
                if (message == null) {
                    break;
                }
                System.out.println("从Queue同步收到的消息：" + message.getText());
            }

           /*异步接收消息
           consumer.setMessageListener(new MessageListener() {
                public void onMessage(Message msg) {
                    if (msg != null) {
                        TextMessage message = (TextMessage) msg;
                        try {
                            System.out.println("从Queue异步收到的消息："+message.getText());
                        } catch (JMSException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            // 休眠100ms再关闭
            Thread.sleep(1000 * 100);*/
            session.commit();
        }   catch (Exception e) {
       e.printStackTrace();
    } finally {
        // 关闭释放资源
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }

    }
}
